#region copyright
/*
* Copyright (c) 2008, Dion Kurczek
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY DION KURCZEK ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL DION KURCZEK BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace SCG.TurboSprite
{
    public class AnimatedBitmapSprite : Sprite
    {
        //Constructor - pass a GPSF, and desired row to use
        public AnimatedBitmapSprite(GamePieceBitmapFactory gpbf, int row)
        {
            _gpbf = gpbf;
            _widthHalf = gpbf.CellWidth / 2;
            _heightHalf = gpbf.CellHeight / 2;
            _row = row;
            Shape = new System.Drawing.RectangleF(-gpbf.CellWidth / 2, -gpbf.CellHeight / 2, gpbf.CellWidth, gpbf.CellHeight);
        }

        //The row to use
        public int Row
        {
            get
            {
                return _row;
            }
            set
            {
                if (value < _gpbf.CellsY)
                    _row = value;
            }
        }

        //The latency, number of cycles to wait before advancing frame
        public int FrameLatency
        {
            get
            {
                return _latency;
            }
            set
            {
                _latency = value;
                _nextFrameLatency = value;
            }
        }

        //Allow access to the frame
        public int Frame
        {
            get
            {
                return _frame;
            }
            set
            {
                _frame = value;
            }
        }

        //Should the sequence of frames be randomized each advance?
        public bool RandomizeFrames { get; set; }

        //Should the delay between frames be randomized each time?
        public bool RandomizeFrameDelay { get; set; }

        //Render the sprite
        public override void Render(Graphics g)
        {
            //advance the counter/frame
            _counter++;
            if (_counter >= _nextFrameLatency)
            {
                _counter = 0;

                //Advance to next frame, either sequential or random
                if (RandomizeFrames)                
                    _frame = RND.Next(_gpbf.CellsX);                
                else
                {
                    _frame++;
                    if (_frame >= _gpbf.CellsX)
                        _frame = 0;
                }

                //determine latency to next frame
                if (RandomizeFrameDelay)
                    _nextFrameLatency = RND.Next(FrameLatency);
            }

            //get the appropriate cell
            Bitmap bmp = _gpbf.GetGamePieceBitmap(_frame, _row);

            //draw it
            g.DrawImage(bmp, X - _widthHalf - Surface.OffsetX, Y - _heightHalf - Surface.OffsetY);
        }

        //private members
        private GamePieceBitmapFactory _gpbf;
        private int _row;
        private int _latency = 10;
        private int _nextFrameLatency;
        private int _counter = 0;
        private int _frame = 0;
        private int _widthHalf;
        private int _heightHalf;
    }
}
